package com.bishe.demo.modules.account.controller;

import com.bishe.demo.modules.account.entity.Role;
import com.bishe.demo.modules.account.service.RoleService;
import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * post
     * localhost/api/role
     * {"roleName":"aaa"}
     */
    @PostMapping(value = "/role", consumes = "application/json")
    ResultEntity<Role> insertRole(@RequestBody Role role) {
        return roleService.insertRole(role);
    }

    /**
     * put
     * localhost/api/role
     * {"roleId":1,"roleName":"bbb"}
     */
    @PutMapping(value = "/role", consumes = "application/json")
    ResultEntity<Role> updateRole(@RequestBody Role role) {
        return roleService.updateRole(role);
    }

    /**
     * delete
     * localhost/api/role/1
     */
    @DeleteMapping("/role/{roleId}")
    ResultEntity<Role> deleteRoleByRoleId(@PathVariable int roleId) {
        return roleService.deleteRoleByRoleId(roleId);
    }

    /**
     * get
     * localhost/api/role/1
     */
    @GetMapping("/role/{roleId}")
    Role getRoleByRoleId(@PathVariable int roleId) {
        return roleService.getRoleByRoleId(roleId);
    }

    /**
     * get
     * localhost/api/role/v2/1
     */
    @GetMapping("/role/v2/{userId}")
    Role getRoleByUserId(@PathVariable int userId) {
        return roleService.getRoleByUserId(userId);
    }

    /**
     * get
     * localhost/api/roles
     */
    @GetMapping("/roles")
    List<Role> getRoles() {
        return roleService.getRoles();
    }

    /**
     * post
     * localhost/api/roles
     * {"keyword":"b"}
     */
    @PostMapping("/roles")
    PageInfo<Role> getRolesBySearchBean(@RequestBody SearchBean searchBean) {
        return roleService.getRolesBySearchBean(searchBean);
    }
}
